package com.lbl.third.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lbl.third.common.BaseContext;
import com.lbl.third.common.R;
import com.lbl.third.enytity.ShoppingCart;
import com.lbl.third.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //设置用户id，指定用户购物车
        Long userId = BaseContext.getCurrentId();

        //设置userid
        shoppingCart.setUserId(userId);
        //构造条件构造器添加过滤条件
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        //获取菜品id
        Long dishId = shoppingCart.getDishId();

        //判断是否添加的为菜品或者套餐
        if(dishId!=null){
            //添加到购物车为菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);


        }else{
            //添加到购物车为套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

        }

        //查询当前菜品或套餐是否在购物车中
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);

        //
        if(one==null){
            //如果不存在，则添加购物车，数量默认为1
          shoppingCart.setNumber(1);
           shoppingCartService.save(shoppingCart);
           one=shoppingCart;
        }else {
            //如果存在，在原来数量的基础上加1
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
        }
       return R.success(one);

    }


    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){

        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        List<ShoppingCart> list = shoppingCartService.list(lambdaQueryWrapper);


        return R.success(list);


    }

    /**
     * 清空购物车
     * @return
     */

    @DeleteMapping("/clean")
    public R<String> clean(){

        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        shoppingCartService.remove(lambdaQueryWrapper);


        return R.success("清空购物车成功");
    }



    /**
     * 减少数量
     * @param shoppingCart
     * @return
     */

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){

        Long currentId = BaseContext.getCurrentId();

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();

        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        if(dishId!=null) {

            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        }else {
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);

        int i = one.getNumber() - 1;

        if(i==0){

            shoppingCartService.removeById(one);
        }else {

            one.setNumber(i);
            shoppingCartService.updateById(one);
        }

        return R.success("操作成功");

    }
}
